package arithmetic.LeetCode;


import static utils.ListNodeUtils.createLink;
import static utils.ListNodeUtils.printLink;

import utils.ListNodeUtils.ListNode;

/**
 * @author jiangfeng on 2023/5/22
 */
public class ReverseList {

    public static void main(String[] args) {

        ListNode link = createLink(new int[] {1, 2, 3, 4, 5});
        printLink(link);

        printLink(new Solution().reverseList(link));
    }
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode() {}
     *     ListNode(int val) { this.val = val; }
     *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     * }
     */
    static class Solution {
        public ListNode reverseList(ListNode head) {
            ListNode pre=null;
            ListNode cur=head;
            while(cur!=null){
                ListNode temp = cur.next;
                cur.next= pre;
                pre = cur;
                cur = temp;
            }
            return pre;

        }
    }
}
